package com.example.communitypro.controller;


import com.example.communitypro.mapper.UserInformationMapper;
import com.example.communitypro.service.ChatService;
import com.example.communitypro.util.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@Api(tags="聊天相关接口")
@RequestMapping("/chat")
public class ChatListController {
    @Autowired
    private UserInformationMapper userInformationMapper;
    @Autowired
    private ChatService chatService;

    @GetMapping(value = "/getChatList")
    @ApiOperation(value = "获取聊天列表")

    public Result updateUserBackground(@RequestParam(value = "userName") Integer userName) {
        return chatService.getSenderChatList(userName);
    }

    @GetMapping(value = "/getMessageRecord")
    @ApiOperation(value = "获取聊天记录(amount:最近多少条消息)")
    public Result getMessageRecord(@RequestParam(value = "sender") Integer sender,
                                   @RequestParam(value = "receiver") Integer receiver,
                                   @RequestParam(value = "amount") int amount) {
        return chatService.getRecentChatRecords(sender,receiver,amount);
    }
    @PostMapping(value = "/setListIdRead")
    @ApiOperation(value = "设置某消息列表已读")

    public Result setUnRead(@RequestParam(value = "listId") int listId) {
        return chatService.setUnRead(listId);
    }

    @PostMapping(value = "/setUserListId")
    @ApiOperation(value = "设置用户ListId")

    public Result setUserListId(@RequestParam(value = "user_no") int user_no,
                                @RequestParam(value = "listId") int listId) {
        return Result.ok().data("result",userInformationMapper.setListId(user_no,listId));
    }
    @PostMapping(value = "/addChatList")
    @ApiOperation(value = "添加聊天列表")

    public Result addChatList(@RequestParam(value = "sender") int sender,
                                @RequestParam(value = "receiver") int receiver) {
        return Result.ok().data("result",chatService.isFirstChat(sender,receiver));
    }
}
